package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows,String sortBy,Boolean desc,String key) {
        PageHelper.startPage(page,rows);
        Example example = new Example(Brand.class);
        if (!StringUtils.isEmpty(key)){
            example.createCriteria().andEqualTo("letter",key.toUpperCase()).orLike("name", "%"+key+"%" );
        }
        if (!StringUtils.isEmpty(sortBy)){
            example.setOrderByClause( sortBy + ( desc ? " DESC" : " ASC"));
        }
        List<Brand> brands = brandMapper.selectByExample(example);
      if (CollectionUtils.isEmpty(brands)){
          throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
      }
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getPages(), BeanHelper.copyWithCollection(brands,BrandDTO.class));
    }

    @Override
    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> ids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brand.setId(null);
        int count = brandMapper.insertSelective(brand);
        if (count!=1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
     count =   brandMapper.insertCategoryAndBrand(brand.getId(),ids);
        if(count!=ids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    @Transactional
    public void editBrand(BrandDTO brandDTO, List<Long> ids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int count = this.brandMapper.updateByPrimaryKeySelective(brand);
        if (count!=1){
            throw new LyException(ExceptionEnum.DATA_EDIT_FAIL);
        }
        this.brandMapper.deleteCategoryAndBrandById(brand.getId());
       count = this.brandMapper.insertCategoryAndBrand(brand.getId(), ids);
        if (count!=ids.size()){
            throw new LyException(ExceptionEnum.DATA_EDIT_FAIL);
        }

    }

    @Override
    @Transactional
    public void deleteBrandById(Long id) {
        this.brandMapper.deleteCategoryAndBrandById(id);
        this.brandMapper.deleteByPrimaryKey(id);
    }


    public BrandDTO queryBrandById(Long id) {
       return BeanHelper.copyProperties(this.brandMapper.selectByPrimaryKey(id),BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryBrandByCategoryId(Long id) {
      List<Brand> list =this.brandMapper.queryBrandByCategoryId(id);
      if (CollectionUtils.isEmpty(list)){
          throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
      }
      return BeanHelper.copyWithCollection(list,BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryBrandByBrandIds(List<Long> ids) {
        List<Brand> brands = this.brandMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands,BrandDTO.class);
    }
}
